package org.example.ai_chat.config;


import org.example.ai_chat.interceptor.LoginvalidateInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    Logger logger = LoggerFactory.getLogger(LoginvalidateInterceptor.class.getName());

    @Autowired
    LoginvalidateInterceptor loginvalidateInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        logger.debug("拦截器的addInterceptors方法被调用");
        registry.addInterceptor(new cn.dev33.satoken.interceptor.SaInterceptor())
                .order(Ordered.HIGHEST_PRECEDENCE)
                .addPathPatterns("/**");
        registry.addInterceptor(loginvalidateInterceptor)
                .order(Ordered.HIGHEST_PRECEDENCE + 1)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login","/user/addUser","/chat/self/memory/chat");

    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

}
